package gui;

import business.EventManager;
import business.InvitationManager;
import business.UserManager;
import entity.Event;
import entity.Invitation;
import entity.InvitationPK;
import entity.User;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import utils.MessageCode;

@Named(value = "invitationListBean")
@SessionScoped
public class InvitationListBean implements Serializable {

    private String usernameToAdd;
    private String usernameToRemove;
    private final Map<String, Integer> eventList;
    private int eventId;

    private String eventName;
    private final List<String> invitationList;
    private String title;
    private String text;

    @EJB
    private UserManager userManager;
    @EJB
    private EventManager eventManager;
    @EJB
    private InvitationManager invitationManager;

    public InvitationListBean() {
        invitationList = new ArrayList<>();
        eventList = new HashMap<>();
    }

    public void init() {
        if (eventId != 0) {
            Event event = eventManager.find(eventId);
            if (event != null) {
                eventName = event.getName() + " / " + event.getDate().toLocaleString();
            }
        }
    }

    public String getUsernameToAdd() {
        return usernameToAdd;
    }

    public void setUsernameToAdd(String usernameToAdd) {
        this.usernameToAdd = usernameToAdd;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getUsernameToRemove() {
        return usernameToRemove;
    }

    public void setUsernameToRemove(String usernameToRemove) {
        this.usernameToRemove = usernameToRemove;
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public List<String> getInvitationList() {
        return invitationList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String[] getCreatedEvents() {
        User currentUser = userManager.getLoggedUser();
        List<Event> createdEvents = userManager.getCreatedEventsFromDate(currentUser, new Date());
        for (Event event : createdEvents) {
            eventList.put(event.getName() + " / " + event.getDate().toLocaleString(), event.getEventId());
        }
        return eventList.keySet().toArray(new String[eventList.keySet().size()]);
    }

    public String addUser() {
        if (!invitationList.contains(usernameToAdd)) {
            invitationList.add(usernameToAdd);
            usernameToAdd = "";
            return "invitationList?faces-redirect=true";
        }
        return "";
    }

    public String removeUser() {
        invitationList.remove(usernameToRemove);
        usernameToRemove = "";
        return "";
    }

    public boolean contains(String username) {
        return invitationList.contains(username);
    }

    public String clear() {
        usernameToAdd = "";
        title = "";
        text = "";
        usernameToRemove = "";
        invitationList.clear();
        return "";
    }

    public String sendInvitations() {
        if (!invitationList.isEmpty()) {
            User sender = userManager.getLoggedUser();
            Event event = eventManager.find(eventList.getOrDefault(eventName, 0));
            List<String> uninvitedUsers = new ArrayList<>();
            for (String username : invitationList) {
                InvitationPK key = new InvitationPK(event.getEventId(), sender.getUsername(), username);
                if (!invitationManager.exists(key)
                        && !event.getParticipants().contains(userManager.find(username))) {
                    Invitation invitation = new Invitation(key);
                    invitation.setTitle(title);
                    invitation.setText(text);
                    invitationManager.create(invitation);
                } else {
                    uninvitedUsers.add(username);
                }
            }
            MessageCode code;
            if (uninvitedUsers.isEmpty()) {
                code = MessageCode.EVERY_USER_INVITED;
            } else if (uninvitedUsers.size() == invitationList.size()) {
                code = MessageCode.NO_USER_INVITED;
            } else {
                code = MessageCode.SOME_USER_INVITED;
            }
            String queryString = "msgCode=" + code.toString();
            if (!uninvitedUsers.isEmpty()) {
                queryString += "&userList=";
                for (String uninvitedUser : uninvitedUsers) {
                    queryString += uninvitedUser + " ";
                }
            }
            return "log?faces-redirect=true&" + queryString;
        }
        return "";
    }
}
